﻿using System;
using System.Linq;
using System.Web.Mvc;
using Beeldbankmvc.Helpers;
using Beeldbankmvc.Models;
using Beeldbankmvc.ViewModels;

namespace Beeldbankmvc.Controllers
{
    [HandleErrorWithELMAH]
    public class KeyWordController : Controller
    {
        KeyWordRepository keyWordRepository = new KeyWordRepository();

        //
        // GET: /KeyWord/a
        [Authorize(Roles="Administrator,MetadataBeheerder")]
        public ActionResult Index(String letter)
        {
            String selectedLetter = "a";

            if (letter != null)
                selectedLetter = letter;

            var keyWords = keyWordRepository.FindAllSorted(selectedLetter).ToList();
            
            return View("Index", keyWords);
        }

        //
        // GET: /KeyWord/Details/5
        [Authorize(Roles="Administrator,MetadataBeheerder")]
        public ActionResult Details(int? id)
        {
            BEELDBANK_WORD keyWord = keyWordRepository.FindOne(id.Value);
            
            return View(keyWord);
        }
        
        //
        // GET: /KeyWord/Create
        [Authorize(Roles = "Administrator,MetadataBeheerder")]
        public ActionResult Create()
        {
            BEELDBANK_WORD keyWord = new BEELDBANK_WORD();

            return View(new KeyWordViewModel(keyWord));
        } 
        
        //
        // POST: /KeyWord/Create
        [HttpPost]
        [Authorize(Roles="Administrator,MetadataBeheerder")]
        public ActionResult Create(FormCollection collection, BEELDBANK_WORD keyWord)
        {
            if (ModelState.IsValid)
            {
                //Add PRICEINFO
                keyWordRepository.Add(keyWord);
                keyWordRepository.Save();

                return RedirectToAction("Details", new { id = keyWord.Id });
            }
            else
                return View(keyWord);
        }
        
        //
        // GET: /KeyWord/Edit/5
        [Authorize(Roles="Administrator,MetadataBeheerder")]
        public ActionResult Edit(int id)
        {
            BEELDBANK_WORD keyWord = keyWordRepository.FindOne(id);

            if (keyWord == null)
                return new FileNotFoundResult { Message = "No word found for id=" + id };

            return View(new KeyWordViewModel(keyWord));
        }

        //
        // POST: /KeyWord/Edit/5
        [HttpPost]
        [Authorize(Roles="Administrator,MetadataBeheerder")]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var keyWord = keyWordRepository.FindOne(id);

            if (ModelState.IsValid)
            {
                //Save keyWord
                UpdateModel(keyWord, "KeyWord", collection); // ,collection
                keyWordRepository.GetDataContext().SubmitChanges();

                return RedirectToAction("Index", new { letter = keyWord.Word[0] });
            }
            else
                return View(new KeyWordViewModel(keyWord));
        }

        //
        // POST: /KeyWord/Delete/5
        [Authorize(Roles="Administrator,MetadataBeheerder")]
        public ActionResult Delete(int id)
        {
            BEELDBANK_WORD keyWord = keyWordRepository.FindOne(id);

            if (keyWord == null)
                return new FileNotFoundResult { Message = "No keyword found for id=" + id };

            try
            {
                //Delete keyWord
                keyWordRepository.Delete(keyWord);
                keyWordRepository.Save();

                return RedirectToAction("Index", new { letter = keyWord.Word[0] });
            }
            catch
            {
                return View();
            }
        }
    }
}
